package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private CategoryBrandRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    //给新增一个关联分类(分类即三级分类)

    /**
     * 思路:
     * 1.从中间表的实体类中取出前端传来的brandId和catelogId
     * 2.通过brandId从pms_brand表中找到brandEntity实体,从这个实体中取出brandName
     * 3.通过catelogId从pms_category表中找到categoryEntity实体,从这个实体中取出catelogName
     * 4.将brandName和catelogName给中间表pms_catelog_brand_relation的brand_name和catelog_name两个记录赋值
     * 这样新增关联分类中中间表的字段有值了,响应给前端就可以了
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();// 从这个中间表的实体中获取brandId
        Long catelogId = categoryBrandRelation.getCatelogId();//  从这个中间表的实体中获取catelogId
        BrandEntity brandEntity = brandDao.selectById(brandId);// 根据brandId查询brand详细信息
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);// 根据catelogId查询分类的详细信息
        // CategoryBrandRelationEntity除了brandId和catelogId外还有 id(自增,不用管),brandName品牌名,catelogName分类名
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        // this.save()就是当前service的save(),即CategoryBrandRelationService.save();
        this.save(categoryBrandRelation);// 保存
    }

    // 更新关联表的品牌信息
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    // 级联更新所有关联的数据
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);// baseMapper就是自己的dao
    }

    /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     * 请求参数:catId:三级分类的id
     * 响应数据:brandId:品牌的id  brandName
     * 思路:
     * 1.在pms_catelog_brand_relation中间表根据catlog_id查到中间表实体
     * 2.从中间表实体中取出brand_id
     * 3.通过brand_id去pms_brand表中拿到brand实体,brandName和brandId不就有了?
     */
    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {
        /*思路1*/
        List<CategoryBrandRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        /*思路2*/
        List<Long> brandIds = relationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        /*思路3*/
        List<BrandEntity> brandEntities = brandDao.selectBatchIds(brandIds);
        return brandEntities;
    }

}